import React, { Component } from 'react'
import DataHoc from "../template/data-hoc";
import AdvancedTable from "components/advanced-table";
import { messages } from "share/common";
import httpFetch from "share/httpFetch";

@DataHoc
class PayFinished extends Component {

  key = 'pay_finished';

  componentDidMount() {
    const search = this.advancedTableRef.advancedSearchRef;
    search.props.form.setFieldsValue(this.props.searchParams);
    this.setState({ status: this.props.searchParams.documentCategory }, search.search);
  }

  componentWillReceiveProps = nextProps => {
    if (nextProps.nowStatus !== this.props.nowStatus && nextProps.nowStatus === 'Paid') {
      const search = this.advancedTableRef.advancedSearchRef;
      search.props.form.setFieldsValue(nextProps.searchParams);
      this.setState({ status: nextProps.searchParams.documentCategory }, () => {
        search.search();
        this.setState({ searchForm: this.props.getSearchForm(this.key, nextProps.searchParams.documentCategory) })
      });
    }
  };

  state = {
    searchForm: this.props.getSearchForm(this.key, 'EXP_REPORT'),
    status: 'EXP_REPORT',
    sortDTOs: []
  };

  handleChangeStatus = value => {
    this.setState({ searchForm: this.props.getSearchForm(this.key, value), status: value }, this.props.handleChangeStatus);
  };

  service = (page, size, searchParams) => {
    const { status, sortDTOs } = this.state;
    let url = status === 'EXP_REPORT' ? '/api/payment/schedule/query/paid' : '/api/loan/line/query/paid';
    return httpFetch.post(url, { page, size, ...searchParams, sortDTOs })
  };

  render() {
    const { searchForm, status } = this.state;
    const { getColumns, handleTableChange, renderAllExpandedRow, handleRowClick, onGetData } = this.props;
    return(
      <div style={{ height: '100%' }}>
        <AdvancedTable
          title={messages('finance-4.key318')/*已付款*/}
          tableName='pay_finished'
          service={this.service}
          searchForm={searchForm}
          rowKey="id"
          onRef={ref => this.advancedTableRef = ref}
          onGetData={onGetData}
          onReset={() => this.setState({ status: 'EXP_REPORT' })}
          index
          onRow={ record => ({ onClick: () => handleRowClick(record, status) })}
          columns={getColumns(this.key)}
          expandedRowRender={renderAllExpandedRow}
          onChange={handleTableChange}
          notLoadWhenMount
        />
      </div>
    )
  }

}

export default PayFinished
